package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.Follow;
import com.net.tkc.pro.entity.ResultEntity;
import com.net.tkc.pro.entity.Take;
import com.net.tkc.pro.impl.ShareImpl;
import com.net.tkc.pro.impl.TakeImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Take/")
public class TakeController {
    ResultEntity resultEntity = null;
    @Resource
    TakeImpl takeImpl;

    /**
     * 根据id删除评论
     * @param id 评论id
     * @return
     */
    @DeleteMapping("Take")
    private String delTake(@RequestParam("id") int id){
        resultEntity = new ResultEntity();
        try{
            int result = takeImpl.delTake(id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 查看帖子
     * @param postid 帖子id
     * @return
     */
    @GetMapping("GetPost")
    private String getPostList(
            @RequestParam("postid") int postid
    ){
        resultEntity = new ResultEntity();
        try{
            List<Take> list=takeImpl.getPostList(postid);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 更改状态
     * @param examine 状态
     * @param id id
     * @return
     */
    @PutMapping("Take")
    private String upExamine(
            @RequestParam("examine") int examine,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = takeImpl.upExamine(examine,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("取消关注成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("取消关注失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 发布评论
     * @param postid 帖子id
     * @param userid 发布评论的人
     * @param comment 评论内容
     * @return
     */
    @PostMapping("Take")
    private String addTake(
            @RequestParam("postid") int postid,
            @RequestParam("userid") int userid,
            @RequestParam("comment") String comment
    ){
        resultEntity = new ResultEntity();
        Take take=new Take();
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date=new Date();
        String Time=dateFormat.format(date);
        take.setDttime(Time);
        take.setPostid(postid);
        take.setUserid(userid);
        take.setComment(comment);
        try{
            int result = takeImpl.addTake(take);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 后台管理系统无条件查询所有
     * @return
     */
    @GetMapping("Get")
    private String getList(
            @RequestParam("limit") int limit,
            @RequestParam("page") int page
    ){
        resultEntity = new ResultEntity();
        try{
            int Start =(page-1)*limit;
            List<Take> list=takeImpl.getTake(limit,Start);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
